package com.example.administrator.qimo3no74.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.qimo3no74.R;
import com.example.administrator.qimo3no74.category.CategoryItem;
import com.example.administrator.qimo3no74.category.CategoryItemAdapter;
import com.example.administrator.qimo3no74.category.fragment.OneFragment;
import com.example.administrator.qimo3no74.category.fragment.OthersFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryFragment extends Fragment {//implements CategoryItemAdapter.MyListener {
    private List<CategoryItem> categoryItems = new ArrayList<>();
    private String itemName;

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public CategoryFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_category, container, false);

        //界面左侧分类
        initCategory();
        actionCategory(view);

        replaceFragment(new OneFragment());

        return view;
    }

    private void actionCategory(View view) {
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.categoryRecyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        CategoryItemAdapter adapter = new CategoryItemAdapter(categoryItems);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new CategoryItemAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, CategoryItem data,int position) {
                setItemName(data.getItemName());

                if(position==0){
                    replaceFragment(new OneFragment());
                }else{
                    replaceFragment(new OthersFragment(itemName));
                }
            }
        });

    }

    private void initCategory() {
        categoryItems.add(new CategoryItem("手机数码"));
        categoryItems.add(new CategoryItem("男装"));
        categoryItems.add(new CategoryItem("鞋靴"));
        categoryItems.add(new CategoryItem("美妆洗护"));
        categoryItems.add(new CategoryItem("图书乐器"));
        categoryItems.add(new CategoryItem("女装"));
        categoryItems.add(new CategoryItem("人群偏爱"));
        categoryItems.add(new CategoryItem("食品"));
        categoryItems.add(new CategoryItem("百货"));
        categoryItems.add(new CategoryItem("家电"));
        categoryItems.add(new CategoryItem("内衣配饰"));
        categoryItems.add(new CategoryItem("花呗分期"));
        categoryItems.add(new CategoryItem("母婴"));
        categoryItems.add(new CategoryItem("家居家纺"));
        categoryItems.add(new CategoryItem("箱包"));
        categoryItems.add(new CategoryItem("家装"));
        categoryItems.add(new CategoryItem("运动户外"));
        categoryItems.add(new CategoryItem("整车车品"));
        categoryItems.add(new CategoryItem("生活服务"));
        categoryItems.add(new CategoryItem("珠宝配饰"));

    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getChildFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.categoryFrameLayout,fragment);
        fragmentTransaction.commit();
    }


//    @Override
//    public void massage(int position,String itemName) {
//        this.index = position;
//        this.itemName = itemName;
//    }
}
